<?php

namespace app\index\controller;
use app\need\controller\Common;
use think\Session;

class Index extends \think\Controller
{
    public $domain_name = 'api.vote.w293.cn';
    public $thisBaseUrl = 'http://api.vote.w293.cn';

    public function index()
    {
        $c = new Common();

//        $Act = new Amg();
        $api_list = array_merge($this->api);

        if (!empty(input('getJson'))) {
            return $c->toJson(0, $api_list, '接口列表');
        }

        $tableHtml = $c->ArrayToTableHtml($api_list);
        $this->assign('tableHtml', $tableHtml);

        $baseUrl = 'https://' . $_SERVER['HTTP_HOST'] . '/';
        $this->assign('baseUrl', $baseUrl);

        $apiUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $this->api[0]['urlTail'];
        if (!empty(Session::get('apiUrl'))) {
            $apiUrl = Session::get('apiUrl');
        }
        $this->assign('apiUrl', $apiUrl);

        return $this->fetch('api_list');
    }

    function instructions()
    {
        return $this->fetch('api_instructions');
    }

    public $api = [
        [//0 :'键名' =>['描述', '参数类型', 是1否0必传, 示范值]
            'apiName' => '接口基本说明',
            'urlTail' => 'index/index/instructions',
            'param' => []
        ],
        [//0 :'键名' =>['描述', '参数类型', 是1否0必传, 示范值]
            'apiName' => '查看文件夹',
            'urlTail' => 'index/index/viewFiles',
            'param' => []
        ]
    ];

    function viewFiles($path = '')
    {
//        $c = new M_common();
//        $c->printR(input(), 'input()');

        $localPath = '/data/wwwroot/h5.52g3.cc/' . $path;
        if ($handle = opendir($localPath)) {
            echo '<style>a {text-decoration: none;color: #000;}</style>';
            echo "<ul>";
            $queue = [];
            while (false !== ($file = readdir($handle))) {
                if ($file != '.' && $file != '..')
                    array_push($queue, $file);
            }
            echo '当前路径：' . $localPath . '<br>';
            $this->printQueue($queue);
            echo "</ul>";
//            Session::set('thisDirFiles', $queue);
            $this->assign('thisDirFiles', $queue);
            $this->assign('fileCount', count($queue));
        }
//        $localPath = str_replace('data/wwwroot/','http://',$localPath);
        $this->assign('dir', $path);


        return $this->fetch('upload');
    }

    function printQueue($queue)
    {
        $fileNameStr = '';
        foreach ($queue as $file) {
//            $this->printFile($file,$fileNameStr);
            $baseUrl = 'http://' . $_SERVER['HTTP_HOST'];
            $currentUrl = $baseUrl . '/' . $this->api[1]['urlTail'];
            $file_path = '/data/wwwroot/h5.52g3.cc/' . input('path') . '/' . $file;
            $path = empty(input('path')) ? $file : input('path') . '/' . $file;
            $fullUrl = $currentUrl . '?path=' . $path;
            $result = is_file($file_path);
            echo '<li>';
            if ($result == 1) {
                $fileNameStr .= $file . ',';
                $file_url = 'http://h5.52g3.cc/' . $path;
                if (getimagesize($file_url)) {
                    $img_html = '<img src="' . $file_url . '" width="40" height="40" style="border:1px #ddd solid"> ';
                    echo $img_html . $file_url;
                } else {
                    $img_html = '<img src="http://h5.52g3.cc/icon/file_icon.png" width="40" height="40"> ';
                    echo '<a href="' . $file_url . '" >' . $img_html . $file_url . '</a>';
                }
            } else {
                echo '<a href="' . $fullUrl . '" >' .
                    '<img src="http://h5.52g3.cc/icon/folder_icon.png" width="40" height="40"> ' .
                    $file . '</a>';
            }
            echo '</li>';
        }
        $this->assign('fileNameStr', $fileNameStr);
    }

    function printFile($file)
    {
        $baseUrl = 'http://' . $_SERVER['HTTP_HOST'];
        $currentUrl = $baseUrl . '/' . $this->api[1]['urlTail'];
        $file_path = '/data/wwwroot/h5.52g3.cc/' . input('path') . '/' . $file;
        $path = empty(input('path')) ? $file : input('path') . '/' . $file;
        $fullUrl = $currentUrl . '?path=' . $path;
        $result = is_file($file_path);
        echo '<li>';
        if ($result == 1) {
            $file_url = 'http://h5.52g3.cc/' . $path;
            if (getimagesize($file_url)) {
                $img_html = '<img src="' . $file_url . '" width="40" height="40" style="border:1px #ddd solid"> ';
                echo $img_html . $file_url;
            } else {
                $img_html = '<img src="http://h5.52g3.cc/icon/file_icon.png" width="40" height="40"> ';
                echo '<a href="' . $file_url . '" >' . $img_html . $file_url . '</a>';
            }
        } else {
            echo '<a href="' . $fullUrl . '" >' .
                '<img src="http://h5.52g3.cc/icon/folder_icon.png" width="40" height="40"> ' .
                $file . '</a>';
        }
        echo '</li>';
    }

}
